<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class admin extends CI_Controller {
	public function index()
    {
    	$data['controller']='user/';
    	$data['name_view']='admin';
    	$data['succes']="";
   		if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    		$userName=$_POST['userName'];
    		$password=$_POST['password'];
    		//$user = $this->user_model->getUserByUserName($userName);
    		
			$filename="./feed/user/admin.xls"; 
			require_once 'excel_reader2.php'; 
			$dataFile = new Spreadsheet_Excel_Reader($filename,true,"UTF-8"); 
			$rowsnum = $dataFile->rowcount($sheet_index=0);
			$colsnum =  $dataFile->colcount($sheet_index=0); 
			$arraytest = array();
			for ($i=2;$i<=$rowsnum;$i++){
				$arraytest[] = array($dataFile->val($i,1),$dataFile->val($i,2)); 
			} 	
			$login = false;
			foreach ($arraytest as $user){
				if($user[0]==$userName && $user[1] == $password){
					$login = true;
				}
			}
			
			if($login==false){
				$data['succes']="Login unsuccessfully";
			}else{   		
    			$userArray = array(
                   'username'  => $userName,
                   'password'     => $password,
            	);
            	$data['userName']=$userName;
				$data['controller']='';
    			$data['name_view']='home';	
    			$this->session->set_userdata($userArray);
    			redirect('c=admin&m=configGallary');
			}  
    	}
    	$this->load->view('template',$data);
    }
    public function configGallary(){
    		
    		$session = $this->session->all_userdata();
    		if(isset($session['username']) && isset($session['password'])){
    			$userName = $session['username'];
    			$password = $session['password'];
	    		$filename="./feed/user/admin.xls"; 
				require_once 'excel_reader2.php'; 
				$dataFile = new Spreadsheet_Excel_Reader($filename,true,"UTF-8"); 
				$rowsnum = $dataFile->rowcount($sheet_index=0);
				$colsnum =  $dataFile->colcount($sheet_index=0); 
				$arraytest = array();
				for ($i=2;$i<=$rowsnum;$i++){
					$arraytest[] = array($dataFile->val($i,1),$dataFile->val($i,2)); 
				} 	
				$login = false;
				foreach ($arraytest as $user){
					if($user[0]==$userName && $user[1] == $password){
						$login = true;
					}
				}
				if($login == false){
					redirect('c=admin');
				}
				
    		}else{
    			redirect('c=admin');
    		}
    		$query = $this->db->query('SELECT * FROM photo');
			$photoArray=array();
			$photoArray=$query->result_array();	
			$countPhoto = count($photoArray);
	    	if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	    		if(isset($_FILES ['image']['name'])){
		    		$image = $_FILES ['image']['name'];
					if ($_FILES["image"]["type"] == "image/jpeg")
					{
					  if ($_FILES["image"]["error"] > 0)
						{
							echo "Error: " . $_FILES["image"]["error"] . "<br />";
						}
					}
					else{
						
						redirect('c=admin&m=configGallary');
					}
					$image = substr($image,0,-4);
					$time = time();
					$image = sha1($image.$time);
			        $uploads_dir = 'feed/photos/';
					$image=$image . ".jpg";				
					move_uploaded_file($_FILES["image"]["tmp_name"],"$uploads_dir" . $image);
					$photoData = array(
						'name'=>$image,
					);
					$this->db->insert('photo', $photoData);
					redirect('c=admin&m=configGallary');
	    		}
	    	}
	    	$data['controller']='user/';
	    	$data['name_view']='config';
	    	$data['photoArray']=$photoArray;
	    	$data['countPhoto'] = $countPhoto;
	    	$this->load->view('template',$data);
   		
    }
    public function delete(){
    	$this->load->model('user_model', null, true);
    	if($_SERVER['REQUEST_METHOD'] == 'GET'){
    		$photoId= $_GET['photoID'];
    		$this->db->delete('photo', array('id' => $photoId)); 
    	}
    	redirect('c=admin&m=configGallary');
    }
    
	public function loadConfig()
	{
		$this->load->model('user_model',null,true);
		if($_SERVER['REQUEST_METHOD'] == 'GET'){
			$query = $this->db->query('SELECT * FROM config_setting');
			$configArray=array();
			$configArray=$query->result_array();		
		}elseif ($_SERVER['REQUEST_METHOD'] == 'POST'){
			$lenght_value=$_POST["lenght_value"];
			$id= $_POST['id'];
			$color_value=$_POST["color_value"];
			$array =array(
				'lenght'=> $lenght_value,
				'color' => $color_value,
			);
			$this->user_model->updateConfigLength($id,$array);
			$query = $this->db->query('SELECT * FROM config_setting');
			$configArray=array();
			$configArray=$query->result_array();
		}
		$data['controller']='user/';
		$data['name_view']='skinconfig';
		$data['configArray']=$configArray;
		$this->load->view('template',$data);
	}
	public function configTravelAndteachingExcel(){    		
    		$session = $this->session->all_userdata();
    		if(isset($session['username']) && isset($session['password'])){
    			$userName = $session['username'];
    			$password = $session['password'];
	    		$filename="./feed/user/admin.xls"; 
				require_once 'excel_reader2.php'; 
				$dataFile = new Spreadsheet_Excel_Reader($filename,true,"UTF-8"); 
				$rowsnum = $dataFile->rowcount($sheet_index=0);
				$colsnum =  $dataFile->colcount($sheet_index=0); 
				$arraytest = array();
				for ($i=2;$i<=$rowsnum;$i++){
					$arraytest[] = array($dataFile->val($i,1),$dataFile->val($i,2)); 
				} 	
				$login = false;
				foreach ($arraytest as $user){
					if($user[0]==$userName && $user[1] == $password){
						$login = true;
					}
				}
				if($login == false){
					redirect('c=admin');
				}
				
    		}else{
    			redirect('c=admin');
    		}
    		$query = $this->db->query('SELECT * FROM travel_teaching');
			$fileArray=array();
			$fileArray=$query->result_array();	
			$wrongError ="";
	    	if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	    		if(isset($_FILES ['excel']['name'])){
		    		$fileExcel= $_FILES ['excel']['name'];
		    		
					if ($_FILES["excel"]["type"] == "application/vnd.ms-excel")
					{
					  if ($_FILES["excel"]["error"] > 0)
						{
							echo "Error: " . $_FILES["excel"]["error"] . "<br />";
						}
					}
					else{
						redirect('c=admin&m=configTravelAndteachingExcel');
					}
					
					$fileExcel = substr($fileExcel,0,-4);
					$time = time();
					$fileExcel = date('Ymdhms');
					$fileExcelName = date('Y-m-d-h-m-s');
			        $uploads_dir = 'feed/travel/';
					$fileExcel=$fileExcel . ".xls";				
					move_uploaded_file($_FILES["excel"]["tmp_name"],"$uploads_dir" . $fileExcel);
					
					$excelData = array(
						'name'=>$fileExcelName,
					);
	    			$filename="./feed/travel/".$fileExcel; 
					require_once 'excel_reader2.php'; 
					$dataExcel = new Spreadsheet_Excel_Reader($filename,true,"UTF-8"); 
					$rowsnum = $dataExcel->rowcount($sheet_index=0); 
					$colsnum =  $dataExcel->colcount($sheet_index=0); 
					$arraytest1 = array();
					if($dataExcel->val(1,1)!='teacher' || $dataExcel->val(1,2)!='from_date' 
					|| $dataExcel->val(1,3)!='to_date' || $dataExcel->val(1,4)!='subject' ||  
					$dataExcel->val(1,5)!='year' || $dataExcel->val(1,6)!='country' || 
					$dataExcel->val(1,7)!='study_break' || $dataExcel->val(1,8)!='from_date_break' || 
					$dataExcel->val(1,9)!='to_date_break'){
						$wrongError = 'The data has wrong structure !';
					}else{
						$this->db->insert('travel_teaching', $excelData);
						redirect('c=admin&m=configTravelAndteachingExcel');
					}
					
	    		}
	    	}
	    	$data['controller']='user/';
	    	$data['name_view']='travelConfig';
	    	$data['fileArray']=$fileArray;
	    	$data['error']= $wrongError;
	    	$this->load->view('template',$data);  		
    }
    public function deleteExcel(){
    	$this->load->model('user_model', null, true);
    	if($_SERVER['REQUEST_METHOD'] == 'GET'){
    		$fileID= $_GET['fileID'];
    		$this->db->delete('travel_teaching', array('id' => $fileID)); 
    	}
    	redirect('c=admin&m=configTravelAndteachingExcel');
    } 
    
	public function configClass(){    		
    		$session = $this->session->all_userdata();
    		if(isset($session['username']) && isset($session['password'])){
    			$userName = $session['username'];
    			$password = $session['password'];
	    		$filename="./feed/user/admin.xls"; 
				require_once 'excel_reader2.php'; 
				$dataFile = new Spreadsheet_Excel_Reader($filename,true,"UTF-8"); 
				$rowsnum = $dataFile->rowcount($sheet_index=0);
				$colsnum =  $dataFile->colcount($sheet_index=0); 
				$arraytest = array();
				for ($i=2;$i<=$rowsnum;$i++){
					$arraytest[] = array($dataFile->val($i,1),$dataFile->val($i,2)); 
				} 	
				$login = false;
				foreach ($arraytest as $user){
					if($user[0]==$userName && $user[1] == $password){
						$login = true;
					}
				}
				if($login == false){
					redirect('c=admin');
				}
				
    		}else{
    			redirect('c=admin');
    		}
    		$query = $this->db->query('SELECT * FROM class');
			$fileArray=array();
			$fileArray=$query->result_array();	
			$wrongError ="";
	    	if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	    		if(isset($_FILES ['excel']['name'])){
		    		$fileExcel= $_FILES ['excel']['name'];
		    		
					if ($_FILES["excel"]["type"] == "application/vnd.ms-excel")
					{
					  if ($_FILES["excel"]["error"] > 0)
						{
							echo "Error: " . $_FILES["excel"]["error"] . "<br />";
						}
					}
					else{
						redirect('c=admin&m=configClass');
					}
					$fileExcel = substr($fileExcel,0,-4);
					$time = time();
					$fileExcel = date('Ymdhms');
					$fileExcelName = date('Y-m-d-h-m-s');
			        $uploads_dir = 'feed/class/';
					$fileExcel=$fileExcel . ".xls";				
					move_uploaded_file($_FILES["excel"]["tmp_name"],"$uploads_dir" . $fileExcel);
					
					$excelData = array(
						'name'=>$fileExcelName,
					);
	    			$filename="./feed/class/".$fileExcel; 
					require_once 'excel_reader2.php'; 
					$dataExcel = new Spreadsheet_Excel_Reader($filename,true,"UTF-8"); 
					$rowsnum = $dataExcel->rowcount($sheet_index=0); 
					$colsnum =  $dataExcel->colcount($sheet_index=0); 
					$arraytest1 = array();
					if($dataExcel->val(1,1)!='subject' || $dataExcel->val(1,2)!='class' 
					|| $dataExcel->val(1,3)!='the_examination' || $dataExcel->val(1,4)!='date' ||  
					$dataExcel->val(1,5)!='room' || $dataExcel->val(1,6)!='seem' || 
					$dataExcel->val(1,7)!='year' || $dataExcel->val(1,8)!='note' || 
					$dataExcel->val(1,9)!='specialization'){
						$wrongError = 'The data has wrong structure !';
					}else{
						$this->db->insert('class', $excelData);
						redirect('c=admin&m=configClass');
					}
					
	    		}
	    	}
	    	$data['controller']='user/';
	    	$data['name_view']='classConfig';
	    	$data['fileArray']=$fileArray;
	    	$data['error']= $wrongError;
	    	$this->load->view('template',$data);  		
    }
    public function deleteExcelClass(){
    	$this->load->model('user_model', null, true);
    	if($_SERVER['REQUEST_METHOD'] == 'GET'){
    		$fileID= $_GET['fileID'];
    		$this->db->delete('class', array('id' => $fileID)); 
    	}
    	redirect('c=admin&m=configClass');
    }
}
?>